<!--管理员搜索账号-->
<link rel="stylesheet" href="http://at.alicdn.com/t/font_1473973_dzwrn7pl4yb.css">
<link rel="stylesheet" href="./css/adminsearch.css">

<!--添加账号模态框-->
<div class="modal fade" id="myModal_add" tabindex="-1" role="dialog" aria-labelledby="myModal_add">
	<div class="modal-dialog" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
				<h4 class="modal-title">添加账号</h4>
			</div>
			<div class="modal-body">
				<div class="input-group">
					<span class="input-group-addon">账号</span>
					<input id="zhanghao" type="text" class="form-control" placeholder="请输入账号" autocomplete="off">
				</div>
				<div class="input-group">
					<span class="input-group-addon">姓名</span>
					<input id="zhanghao_name" type="text" class="form-control" placeholder="请输入姓名">
				</div>
				<div class="input-group">
					<span class="input-group-addon">密码</span>
					<input id="zhaghao_passwd" type="password" class="form-control" placeholder="请输入密码(可选)" autocomplete="off">
				</div>
				<div class="input-group margintop" id="bishe_type_display1" style="display: none;">
					<span class="input-group-addon">类型</span>
					<select id="bishe_type1" class="bishe">
						<option value="0">普通老师</option>
						<option value="1">毕设老师</option>
					</select>
				</div>
				<div class="radio text-left">
					<label><input type="radio" name="xuanze2" value="0" checked="checked">学生</label>
					<label><input type="radio" name="xuanze2" value="1">教师</label>
				</div>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
				<button type="button" class="btn btn-primary" data-dismiss="modal" id="ok_add">确定添加</button>
			</div>
		</div>
	</div>
</div>

<!--修改账号模态框-->
<div class="modal fade" id="myModal_xiugai" tabindex="-1" role="dialog" aria-labelledby="myModal_xiugai">
	<div class="modal-dialog" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
				<h4 class="modal-title">修改账号</h4>
			</div>
			<div class="modal-body">
				<div class="input-group">
					<span class="input-group-addon">账号</span>
					<input id="xiugai_zhanghao" type="text" class="form-control" placeholder="请输入账号">
				</div>
				<div class="input-group">
					<span class="input-group-addon">姓名</span>
					<input id="xiugai_name" type="text" class="form-control" placeholder="请输入姓名">
				</div>
				<div class="input-group">
					<span class="input-group-addon">密码</span>
					<input id="xiugai_passwd" type="password" class="form-control" placeholder="请输入密码(可选)" autocomplete="off">
				</div>
				<div class="input-group margintop" id="bishe_type_display2" style="display: none;">
					<span class="input-group-addon">类型</span>
					<select id="bishe_type2" class="bishe">
						<option value="0">普通老师</option>
						<option value="1">毕设老师</option>
					</select>
				</div>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
				<button type="button" class="btn btn-primary" data-dismiss="modal" id="ok_xiugai">确定修改</button>
			</div>
		</div>
	</div>
</div>

<h3>搜索账号<button class="btn btn-primary btnn" id="zhanghao_add">添加账号</button>
	       <button class="btn btn-primary btnn" id="zhanghao_pxadd">批量导入账号</button>
</h3>
<div class="input-group margintop">
	<span class="input-group-addon" id="search"><span class="iconfont icon-sousuo"></span></span>
	<input type="text" id="search1" class="form-control" placeholder="请输入要搜索的账号(支持模糊查询)" aria-describedby="sizing-addon1">
</div>

<div class="radio text-left">
	<label><input type="radio" name="xuanze" value="0" checked>学生</label>
	<label><input type="radio" name="xuanze" value="1">教师</label>
	<button class="btn btn-primary btnn" id="searchok">搜索</button>
</div>

<h3 style="text-align: left;margin-top: 40px;">查询结果</h3>
<table class="table table-striped table-hover table-bordered table-condensed table-responsive" style="width:400px">
	<thead>
		<tr>
			<th>账号</th>
			<th>姓名</th>
			<th>操作</th>
		</tr>
	</thead>
</table>

<script>
	$('input[name="xuanze2"]').change(function() {
		if($('input[name="xuanze2"]:checked').val() == 1) {
			$('#bishe_type_display1').css("display", "table");
		} else {
			$('#bishe_type_display1').css("display", "none");
		}
	})

	$('#zhanghao_add').click(function() {
		$(myModal_add).modal({
			keyboard: true,
			show: true
		})
	})

	$('#ok_add').click(function() {
		var data;
		if($("input[name='xuanze2']:checked").val() == 0) {
			data = {
				account: $('#zhanghao').val(),
				name: $('#zhanghao_name').val(),
				password: $('#zhaghao_passwd').val(),
				flag: $("input[name='xuanze2']:checked").val(),
				sid: "xiangsihu",
				type: -1
			}
		} else {
			data = {
				account: $('#zhanghao').val(),
				name: $('#zhanghao_name').val(),
				password: $('#zhaghao_passwd').val(),
				flag: $("input[name='xuanze2']:checked").val(),
				sid: "xiangsihu",
				type: $('#bishe_type1').val(),
			}
		}

		$.ajax({
			xhrFields: {
				withCredentials: true
			},
			type: 'POST',
			url: getFullURL("admin/add-stuorter"),
			data: data,
			dataType: 'json',
			success: function(data) {
				if(data.status == 0) {
					alert("添加成功！");
				}
			},
			error: function(err) {
				alert("请求异常，请重新登录！", function() {
					$(location).attr('href', './login.html');
				})
			}
		})
	})

	$('#searchok').click(function() {
		var myajax = $.ajax({
			xhrFields: {
				withCredentials: true
			},
			type: 'POST',
			url: getFullURL("admin/search-at"),
			data: {
				account: $('#search1').val(),
				type: $("input[name='xuanze']:checked").val()
			},
			dataType: 'json',
			success: function(data) {
				$.session.set("zhanghao_flag", $("input[name='xuanze']:checked").val());
				if(data.status == 0) {
					$('.table').html("");
					var thead = `
							<thead>
								<tr>
									<th>账号</th>
									<th>姓名</th>
									<th>操作</th>
								</tr>
							</thead>
							`;
					$('.table').append(thead);

					$.each(data.list, function(i, n) {
						var str = `<tr>
									<td>` + n.account + `</td>
									<td>` + n.name + `</td>
									<td>
										<button class="btn btn-primary btn-xs xiugai" value="` + n.account + `">修改</button>
										<button class="btn btn-primary btn-xs shanchu" value="` + n.account + `">删除</button>
									</td>
								</tr>`;
						$('.table').append(str);
					})
				} else if(data.status == -1) {
					alert("无结果！");
				}
			},
			error: function(err) {
				alert("请求异常，请重新登录！", function() {
					$(location).attr('href', './login.html');
				})
			}
		})

		$.when(myajax).done(function() {
			$('.xiugai').click(function() {
				//全局变量
				var path = '';
				var data = '';
				var fill = function() {}

				if($.session.get("zhanghao_flag") == 0) {
					$('#bishe_type_display2').css("display", "none");
					path = 'userInfo';
					data = {
						sno: $(this).val()
					}
					fill = function(data) {
						$('#xiugai_zhanghao').val(data.user.sno);
						$('#xiugai_name').val(data.user.name);
					}
				} else {
					$('#bishe_type_display2').css("display", "table");
					path = 'teacher-info';
					data = {
						tno: $(this).val()
					}
					fill = function(data) {
						$('#xiugai_zhanghao').val(data.teacher.tno);
						$('#xiugai_name').val(data.teacher.name);
						$("#bishe_type2").find("option[value=" + data.teacher.type + "]").attr("selected", true);
					}
				}

				var myajax2 = $.ajax({
					xhrFields: {
						withCredentials: true
					},
					type: 'POST',
					url: getFullURL(path),
					data: data,
					dataType: 'json',
					success: function(data) {
						if(data.status == 0) {
							console.log(data)
							fill(data);
						}
					},
					error: function(err) {
						alert("请求异常，请重新登录！", function() {
							$(location).attr('href', './login.html');
						})
					}
				})

				$.when(myajax2).done(function() {
					$(myModal_xiugai).modal('show')
				})
			})

			$("#ok_xiugai").unbind("click").click(function() {
				//全局变量
				var data = '';

				if($.session.get("zhanghao_flag") == 0) {
					data = {
						account: $('#xiugai_zhanghao').val(),
						name: $('#xiugai_name').val(),
						password: $('#xiugai_passwd').val(),
						flag: $.session.get("zhanghao_flag"),
						type: -1
					}
				} else {
					data = {
						account: $('#xiugai_zhanghao').val(),
						name: $('#xiugai_name').val(),
						password: $('#xiugai_passwd').val(),
						flag: $.session.get("zhanghao_flag"),
						type: $('#bishe_type2').val()
					}
				}

				$.ajax({
					xhrFields: {
						withCredentials: true
					},
					type: 'POST',
					url: getFullURL("admin/update-stuorter"),
					data: data,
					dataType: 'json',
					success: function(data) {
						if(data.status == 0) {
							alert("修改成功！");
							setTimeout(function() {
								$("#searchok").trigger("click");
							}, 300);
						}
					},
					error: function(err) {
						alert("请求异常，请重新登录！", function() {
							$(location).attr('href', './login.html');
						})
					}
				})
			})

			$(".shanchu").unbind("click").click(function() {
				$.ajax({
					xhrFields: {
						withCredentials: true
					},
					type: 'POST',
					url: getFullURL("admin/del-stuorter"),
					data: {
						account: $(this).val(),
						flag: $.session.get("zhanghao_flag")
					},
					dataType: 'json',
					success: function(data) {
						if(data.status == 0) {
							alert("删除成功！");
							setTimeout(function() {
								$("#searchok").trigger("click");
							}, 300);
						}
					},
					error: function(err) {
						alert("请求异常，请重新登录！", function() {
							$(location).attr('href', './login.html');
						})
					}
				})
			})
		})
	})
</script>